axios.defaults.baseURL = 'http://localhost:8888'
var id = location.search.slice(1).split('=')[1]
var goodsShow = document.querySelector('section')
var cartNum = document.querySelector('header>span')
function goodsDetail(){
    async function getDetail(){
        let res = await axios.get('/goods/item',{params:{id:id},headers:{'content-type':'application/x-www-form-urlencoded','authorization':localStorage.getItem('token')}})
        console.log(res.data)
        var item = res.data.info
        var str = `<article><img src = ${item.img_big_logo}>
                    ${item.is_hot ? '<span>热卖</span>' :''}
                    <div><h1>${item.title}</h1><span>&yen;${item.is_sale ? item.current_price: item.price}</span>
                    <button class="addCart">加入购物车</button>
                    </div>
                    </article>
                    <h1>规格参数</h1>`
        goodsShow.innerHTML = str + item.goods_introduce
        function cartList(){
            axios.get('/cart/list',{params:{id:localStorage.getItem('id')},headers:{'content-type':'application/x-www-form-urlencoded','authorization':localStorage.getItem('token')}}).then(res => {
                console.log(res)
                var allCart = 0;
                res.data.cart.forEach(item => {
                    allCart += item.cart_number
                })
                cartNum.innerText = allCart
            })
        }
        cartList()
        goodsShow.onclick = function(event){
            if(event.target.className === 'addCart'){
                axios.post('/cart/add',{id:localStorage.getItem('id'),goodsId:item.goods_id},{headers:{'content-type':'application/x-www-form-urlencoded','authorization':localStorage.getItem('token')}}).then(res => {
                    console.log(res)
                    cartList()
                })
            }
        }
        
    }
    getDetail()
}
goodsDetail()
cartNum.onclick = () => location.href = 'cart.html'